package com.managedBean.merchant;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.primefaces.event.RowEditEvent;

import com.controller.merchant.OrderController;
import com.dto.merchant.OrderDTO;

@ViewScoped
@ManagedBean(name = "orderManagedBean")
public class OrderManagedBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1478715328736260595L;
	private List<OrderDTO> orderList;
	private SelectItem[] statusOptions;
	@ManagedProperty(value = "#{orderController}")
	private OrderController orderController;

	public OrderManagedBean() {
		orderList = new ArrayList<OrderDTO>();
		statusOptions = new SelectItem[] { new SelectItem("Submitted"),
				new SelectItem("Read"), new SelectItem("Packed"),
				new SelectItem("Routed"), new SelectItem("Delivered") };
	}

	@PostConstruct
	public void init() {
		orderList=orderController.findAll();
	}

	public void getOrders() {
//		System.out.println(new Date().getSeconds());
		init();

	}

	public void onEdit(RowEditEvent event) {
		// TODO update order history/log table and order table
		OrderDTO dto=(OrderDTO) event.getObject();
		orderController.updateOrder(dto);
		init();
		FacesMessage msg = new FacesMessage("Order Edited",
				((OrderDTO) event.getObject()).getOid() + " Status"
						+ ((OrderDTO) event.getObject()).getStatus());
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	/**
	 * @return the orderList
	 */
	public List<OrderDTO> getOrderList() {
		return orderList;
	}

	/**
	 * @param orderList
	 *            the orderList to set
	 */
	public void setOrderList(List<OrderDTO> orderList) {
		this.orderList = orderList;
	}

	/**
	 * @return the statusOptions
	 */
	public SelectItem[] getStatusOptions() {
		return statusOptions;
	}

	/**
	 * @param statusOptions
	 *            the statusOptions to set
	 */
	public void setStatusOptions(SelectItem[] statusOptions) {
		this.statusOptions = statusOptions;
	}

	/**
	 * @return the orderController
	 */
	public OrderController getOrderController() {
		return orderController;
	}

	/**
	 * @param orderController the orderController to set
	 */
	public void setOrderController(OrderController orderController) {
		this.orderController = orderController;
	}

}
